<?php
class imagesMod extends MY_Model{
	var $gallery_path;
  	var $gallery_path_url;
  	
	function imagesMod(){
		parent::__construct(
			"images",
			array(
				"id_images",
				"id_album",
				"id_member",
				"name",
				"images",
				"thumbs",
				"type_images",
				"tag",
				"description",
				"posted",
			),
			array("id_images")
		);
	$this->gallery_path = realpath(APPPATH . '../images/');
    $this->gallery_path_url = base_url().'../images/';
	}
	
	function update($id, $newData){
    	$this->db->where($this->table_fields[0], $id);
		return $this->db->update($this->table_name, $newData); 
    }
	
 	function do_upload() {
    $config = array(
      'allowed_types' => "jpg|jpeg|gif|png",
      'upload_path' => $this->gallery_path,
      'max_size'=>2000
    );
    $this->load->library('upload', $config);
    $this->upload->do_upload(); //do upload
 
    //want to create thumbnail
 
    $image_data = $this->upload->data(); //get image data
 
    $config = array(
      'source_image' => $image_data['full_path'], //get original image
      'new_image' => $this->gallery_path . '/thumbs', //save as new image //need to create thumbs first
      'maintain_ratio' => true,
      'width' => 150,
      'height' => 100
 
    );
 
    $this->load->library('image_lib', $config); //load library
    $this->image_lib->resize(); //do whatever specified in config
  }
	
	function generateID($data){
    	return md5($data.(date("Y-m-d H:m:s")+20));
    }
    
	/**
     * Insert data cuisine (menu makanan) baru. 
     * Contoh penggunaan:
     * </pre>
     * @param $newData (array) - array dari nama fields dan nilanya.
     */
    function Add($newData){
    	$newData["posted"] = date("Y-m-d H:m:s");
    	return $this->db->insert($this->table_name, $newData);
    }
    
    /**
     * value dari tiap primary key field.
     * Contoh :
     * <pre>
     * 		//value dari tiap primary key field
     * 		$data = array(
     * 			'[id 1]',  		// primary key 1
     * 			'[id 2]',  		// primary key 2
     * 		);
     * </pre>
     * @param $leafID (array) - value dari tiap primary fields
     */
    function remove($ids){
    	
    	if (!is_array($ids)) $ids = array($ids);
    	foreach($ids as $pk) {
    		$this->db->or_where("id_images", $pk);	
    	}
    	
    	return $this->db->delete($this->table_name, $pk);
    }
    
    /**
     * return cuisine (menu makanan) dengan id tertentu. 
     * @param $ids (array) - id facility 
     */
    function get($id){
		return $this->db->get_where($this->table_name, array("id_images"=>$id));
    }
    
    function get_order($limit,$offset=20){
    	return $this->db->query("select * from images order by posted desc limit $limit, $offset");
    }
        
	function getImages(){
        $this->db->select('images');
        $this->db->from($this->table_name);
//        $this->db->where('id_images',$id);
        $result = $this->db->get();
//        if($result->num_rows()>0){
//             $row = $result->row();
//             return $row->images;
//        }else{
//             return false;
//        }
        return true;
    }
    
	function getThumbs(){
        $this->db->select('thumbs');
        $this->db->from($this->table_name);
//        $this->db->where('id_images',$id);
//        $result = $this->db->get();
        $this->db->get();
//        if($result->num_rows()>0){
//             $row = $result->row();
//             return $row->thumbs;
//        }else{
//             return false;
//        }
        return true;
    }
    
	function removeImages($table_comment,$id){
    	$this->db->delete($table_comment, array('id_images'=>$id));
//    	$this->db->delete($table_images, array('id_album'=>$id));
    	$this->db->delete($this->table_name, array('id_images'=>$id));
    	return true;
    }
    
	function removeCommentImages($id){
    	$que = $this->db->query("select id_images from images where id_album='$id'");
    	foreach ($que as $res){
    		$this->remove($res->id_images);
//    		$this->db->delete("delete from comment_images where id_images='$res->id_images'");
    	}
    	return true;
    }
    
	function get_album($id_member,$id,$limit,$offset=100){
//		return $this->db->get_where($this->table_name, array('id_album' => $id,'id_member'=>$id_member), $limit, $offset);
		return $this->db->query("select * from images where id_album='$id' and id_member='$id_member'");
    }
	
	function get_albumName(){
        return $this->db->query('select id_album,name from album order by name asc');
    }
}